﻿package feature.app;


import features.OptionalFeature;
import features.StreamFeature;
import features.SwitchExpressions;
import features.TextBlockFeature;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

public class AppController implements Initializable {

    private List<String> list = List.of("OptionalFeature",
            "StreamFeature", "SwitchExpressions", "TextBlockFeature");

    private OptionalFeature optionalFeature = new OptionalFeature();
    private StreamFeature streamFeature = new StreamFeature();
    private SwitchExpressions switchExpressions = new SwitchExpressions();
    private TextBlockFeature textBlockFeature = new TextBlockFeature();

    @FXML
    private ListView<String> featureList;

    @FXML
    private TextField inputField;

    @FXML
    private ListView<String> functionList;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        featureList.getItems().addAll(list);
        featureList.getSelectionModel()
                .selectedItemProperty()
                .addListener((ov, oldValue, newValue) -> {
                    switch (newValue) {
                        case "OptionalFeature" -> mapOptional();
                        case "StreamFeature" -> mapStream();
                        case "SwitchExpressions" -> mapSwitch();
                        default -> mapTextBlock();
                    }
                });

    }

    private void mapOptional() {
        inputField.setEditable(true);
        inputField.setPromptText("string, string, ...");
        functionList.getItems().clear();
        List<String> items =
                Arrays.stream(optionalFeature.getClass().getDeclaredMethods()).map(e-> e.getName()).collect(Collectors.toList());
        functionList.getItems().addAll(items);
        inputField.setOnAction(e -> {
            String[] split = inputField.getText().split(",");
            List<String> strings = Arrays.stream(split).collect(Collectors.toList());
//            if(functionList.get)
                

        });

    }



    private void mapStream() {
        inputField.setEditable(true);
        inputField.setPromptText("string, string, ...");
        functionList.getItems().clear();
        List<String> items =
                Arrays.stream(optionalFeature.getClass().getDeclaredMethods()).map(e-> e.getName()).collect(Collectors.toList());
        functionList.getItems().addAll(items);


    }

    private void mapSwitch() {
        inputField.setEditable(true);
        inputField.setPromptText("single string");
        functionList.getItems().clear();
        List<String> items =
                Arrays.stream(optionalFeature.getClass().getDeclaredMethods()).map(e-> e.getName()).collect(Collectors.toList());
        functionList.getItems().addAll(items);

    }

    private void mapTextBlock() {
        inputField.setEditable(false);
        functionList.getItems().clear();

    }
}
